using System;
using System.Collections;
using System.IO;
using System.Xml;
using LocalBoard.Domain;
using NUnit.Framework;

namespace LocalBoard.Persistence
{
	[NUnit.Framework.TestFixture]
	public class TestAccessorWithMultiClub
	{
		protected IAttachmentAccessor _attachmentAccessor;
		protected IBoardAccessor _boardAccessor;
		protected IClubAccessor _clubAccessor;
		protected ICommentAccessor _commentAccessor;
		protected IMessageAccessor _messageAccessor;
		protected IPollItemAccessor _pollItemAccessor;
		protected IProfileAccessor _profileAccessor;
		protected IUserRoleAccessor _userRoleAccessor;
		protected IClubRoleAccessor _clubRoleAccessor;
		protected IClubProfileAccessor _clubProfileAccessor;

		protected Club _clubOne;
		protected Club _clubTwo;

		[NUnit.Framework.SetUp]
		public void setUp()
		{
			IAccessorFactory accessorFactory = new MockAccessorFactory();
			_attachmentAccessor = accessorFactory.GetAttachmentAccessor();
			_boardAccessor = accessorFactory.GetBoardAccessor();
			_clubAccessor = accessorFactory.GetClubAccessor();
			_commentAccessor = accessorFactory.GetCommentAccessor();
			_messageAccessor = accessorFactory.GetMessageAccessor();
			_pollItemAccessor = accessorFactory.GetPollItemAccessor();
			_profileAccessor = accessorFactory.GetProfileAccessor();
			_userRoleAccessor = accessorFactory.GetUserRoleAccessor();
			_clubRoleAccessor = accessorFactory.GetClubRoleAccessor();
			_clubProfileAccessor = accessorFactory.GetClubProfileAccessor();

			_clubOne = _clubAccessor.Get(1);
			_clubTwo = _clubAccessor.Get(2);
		}
		[NUnit.Framework.TearDown]
		public void tearDown()
		{
			MockAccessorFactory.ResetDataTable();

			_attachmentAccessor = null;
			_boardAccessor = null;
			_clubAccessor = null;
			_commentAccessor = null;
			_messageAccessor = null;
			_pollItemAccessor = null;
			_profileAccessor = null;
			_userRoleAccessor = null;
			_clubRoleAccessor = null;
			_clubProfileAccessor = null;

			_clubOne = null;
			_clubTwo = null;
		}
		[NUnit.Framework.Test]
		public void testBoardAccessor()
		{
			Board testBoard1 = new Board("Board 1",null,0,0,_clubOne.Id,BoardViewType.List);
			Board testBoard2 = new Board("Board 2",null,0,0,_clubOne.Id,BoardViewType.List);
			Board testBoard3 = new Board("Board 3",null,0,0,_clubOne.Id,BoardViewType.List);
			
			_boardAccessor.Insert(testBoard1);
			_boardAccessor.Insert(testBoard2);
			_boardAccessor.Insert(testBoard3);

			ArrayList boardList = _boardAccessor.GetBoardListByClubId(_clubOne.Id);
			NUnit.Framework.Assertion.Assert(boardList.Count == 3);

			Board testBoard1InClubTwo = new Board("Board 1",null,0,0,_clubTwo.Id,BoardViewType.List);
			Board testBoard2InClubTwo = new Board("Board 2",null,0,0,_clubTwo.Id,BoardViewType.List);
			Board testBoard3InClubTwo = new Board("Board 3",null,0,0,_clubTwo.Id,BoardViewType.List);

			Board newBoard = _boardAccessor.Get(_boardAccessor.Insert(testBoard1InClubTwo));
			NUnit.Framework.Assertion.Assert(newBoard.Name == "Board 1");

			newBoard = _boardAccessor.Get(_boardAccessor.Insert(testBoard2InClubTwo));
			NUnit.Framework.Assertion.Assert(newBoard.Name == "Board 2");
			
			newBoard = _boardAccessor.Get(_boardAccessor.Insert(testBoard3InClubTwo));
			NUnit.Framework.Assertion.Assert(newBoard.Name == "Board 3");

			ArrayList boardListInClubTwo = _boardAccessor.GetBoardListByClubId(_clubTwo.Id);
			NUnit.Framework.Assertion.Assert(boardListInClubTwo.Count == 3);

			newBoard.Name = "Board 3 name changed";
			_boardAccessor.Update(newBoard);
			Board changedBoard = _boardAccessor.Get(newBoard.Id);
			NUnit.Framework.Assertion.Assert(changedBoard.Name != "Board 3");

			int profileId = _profileAccessor.Insert(new Profile("testuser", "username", "testpwd", "test@test.com"));
			XmlDocument doc = new XmlDocument();
			doc.LoadXml("<profile>test</profile>");
			_clubProfileAccessor.Insert(profileId, doc, changedBoard.ClubId);
			_boardAccessor.AssignManagerToBoard(profileId, changedBoard.Id);
			changedBoard = _boardAccessor.Get(changedBoard.Id);
			NUnit.Framework.Assertion.Assert(changedBoard.Manager.Id == profileId);

			_boardAccessor.Delete(changedBoard.Id);
			boardList = _boardAccessor.GetBoardListByClubId(_clubTwo.Id);
			NUnit.Framework.Assertion.Assert(boardList.Count == 2);

			_profileAccessor.Delete(profileId);
		}

		[NUnit.Framework.Test]
		public void testClubProfileAccessor()
		{
			Profile testProfile1 = new Profile("userId1","userName 1", "password1","email1@mail.com");
			Profile testProfile2 = new Profile("userId2","userName 2", "password2","email2@mail.com");
			Profile testProfile3 = new Profile("userId3","userName 3", "password3","email3@mail.com");
			Profile testProfile4 = new Profile("userId4","userName 4", "password4","email4@mail.com");

			testProfile1 = _profileAccessor.Get(_profileAccessor.Insert(testProfile1));
			testProfile2 = _profileAccessor.Get(_profileAccessor.Insert(testProfile2));
			testProfile3 = _profileAccessor.Get(_profileAccessor.Insert(testProfile3));
			testProfile4 = _profileAccessor.Get(_profileAccessor.Insert(testProfile4));

			XmlDocument doc = new XmlDocument();
			string xmlStr1 = "<ClubProfile>test1</ClubProfile>";
			doc.LoadXml(xmlStr1);
			XmlNode clubProfile1 = doc.SelectSingleNode("/");
			_clubProfileAccessor.Insert(testProfile1.Id, clubProfile1, _clubOne.Id);
			XmlNode profileNode1 = _clubProfileAccessor.Get(testProfile1.Id, _clubOne.Id);
			Assert.IsNotNull(profileNode1);
			Assert.AreEqual(xmlStr1, profileNode1.OuterXml);

			string xmlStr2 = "<ClubProfile>test2</ClubProfile>";
			doc.LoadXml(xmlStr2);
			XmlNode clubProfile2 = doc.SelectSingleNode("/");
			_clubProfileAccessor.Insert(testProfile2.Id, clubProfile2, _clubTwo.Id);
			XmlNode profileNode2 = _clubProfileAccessor.Get(testProfile2.Id, _clubTwo.Id);
			Assert.IsNotNull(profileNode2);
			Assert.AreEqual(xmlStr2, profileNode2.OuterXml);

			string xmlStr3 = "<ClubProfile>test3</ClubProfile>";
			doc.LoadXml(xmlStr3);
			XmlNode clubProfile3 = doc.SelectSingleNode("/");
			_clubProfileAccessor.Insert(testProfile3.Id, clubProfile3, _clubTwo.Id);
			XmlNode profileNode3 = _clubProfileAccessor.Get(testProfile3.Id, _clubTwo.Id);
			Assert.IsNotNull(profileNode3);
			Assert.AreEqual(xmlStr3, profileNode3.OuterXml);

			Hashtable clubOneProfiles = _clubProfileAccessor.GetAllUserClubProfiles(_clubOne.Id);
			Assert.AreEqual(1, clubOneProfiles.Count);
			Hashtable clubTwoProfiles = _clubProfileAccessor.GetAllUserClubProfiles(_clubTwo.Id);
			Assert.AreEqual(2, clubTwoProfiles.Count);

			string xmlStrUpdate = "<ClubProfile>update</ClubProfile>";
			doc.LoadXml(xmlStrUpdate);
			_clubProfileAccessor.Update(testProfile1.Id, doc, _clubOne.Id);
			profileNode1 = _clubProfileAccessor.Get(testProfile1.Id, _clubOne.Id);
			Assert.AreEqual(xmlStrUpdate, profileNode1.OuterXml);

			_clubProfileAccessor.Delete(testProfile1.Id, _clubOne.Id);
			clubOneProfiles = _clubProfileAccessor.GetAllUserClubProfiles(_clubOne.Id);
			Assert.AreEqual(0, clubOneProfiles.Count);

			Assert.IsFalse(_clubProfileAccessor.IsUserInTheClub(testProfile1.Id, _clubOne.Id));
			Assert.IsFalse(_clubProfileAccessor.IsUserInTheClub(testProfile2.Id, _clubOne.Id));
			Assert.IsTrue(_clubProfileAccessor.IsUserInTheClub(testProfile2.Id, _clubTwo.Id));
		}
		
		[NUnit.Framework.Test]
		public void testUserRoleAccessor()
		{
			int role1Id = _clubRoleAccessor.Insert("role1", _clubOne.Id);
			int role2Id = _clubRoleAccessor.Insert("role2", _clubOne.Id);
			int role3Id = _clubRoleAccessor.Insert("role3", _clubOne.Id);
			int role1IdInTwo = _clubRoleAccessor.Insert("role1InTwo", _clubTwo.Id);
			int role2IdInTwo = _clubRoleAccessor.Insert("role2InTwo", _clubTwo.Id);
			int role3IdInTwo = _clubRoleAccessor.Insert("role3InTwo", _clubTwo.Id);

			Hashtable allRoles = _clubRoleAccessor.GetAllClubRoles(_clubOne.Id);
			Hashtable allRolesInTwo = _clubRoleAccessor.GetAllClubRoles(_clubTwo.Id);
			Assert.IsNotNull(allRoles);
			Assert.AreEqual(allRoles.Count, 3);
			Assert.IsNotNull(allRolesInTwo);
			Assert.AreEqual(allRolesInTwo.Count, 3);

			UserRole role1 = allRoles[role1Id] as UserRole;
			role1.Name = "role1 name changed";
			_clubRoleAccessor.Update(role1, _clubOne.Id);
			allRoles = _clubRoleAccessor.GetAllClubRoles(_clubOne.Id);
			role1 = allRoles[role1Id] as UserRole;
			Assert.AreEqual("role1 name changed", role1.Name);

			_clubRoleAccessor.Delete(role1.Id, _clubOne.Id);
			allRoles = _clubRoleAccessor.GetAllClubRoles(_clubOne.Id);
			NUnit.Framework.Assertion.Assert(allRoles != null && allRoles.Count == 2);

			int profileId = _profileAccessor.Insert(new Profile("userId4","userName 4", "password4","email4@mail.com"));
			_clubProfileAccessor.Insert(profileId, null, _clubOne.Id);
			_clubProfileAccessor.Insert(profileId, null, _clubTwo.Id);
			_clubRoleAccessor.AddClubRoleToProfile(role2Id, profileId, _clubOne.Id);
			_clubRoleAccessor.AddClubRoleToProfile(role3Id, profileId, _clubOne.Id);
			_clubRoleAccessor.AddClubRoleToProfile(role3IdInTwo, profileId, _clubTwo.Id);
			ArrayList profileRoles = _clubRoleAccessor.GetClubRolesByProfileId(profileId, _clubOne.Id);
			ArrayList profileRolesInTwo = _clubRoleAccessor.GetClubRolesByProfileId(profileId, _clubTwo.Id);
			NUnit.Framework.Assertion.Assert(profileRoles != null && profileRoles.Count == 2);
			NUnit.Framework.Assertion.Assert(profileRolesInTwo != null && profileRolesInTwo.Count == 1);

			int profileId2 = _profileAccessor.Insert(new Profile("userId5","userName 5", "password5","email5@mail.com"));
			_clubProfileAccessor.Insert(profileId2, null, _clubOne.Id);
			_clubRoleAccessor.AddClubRoleToProfile(role2Id, profileId2, _clubOne.Id);
			ArrayList profileRoles2 = _clubRoleAccessor.GetClubRolesByProfileId(profileId2, _clubOne.Id);
			Assert.IsNotNull(profileRoles2);
			Assert.AreEqual(1, profileRoles2.Count);

			_clubRoleAccessor.DeleteClubRoleFromProfile(role2Id, profileId, _clubOne.Id);
			profileRoles = _clubRoleAccessor.GetClubRolesByProfileId(profileId, _clubOne.Id);
			NUnit.Framework.Assertion.Assert(profileRoles != null && profileRoles.Count == 1);

			_clubRoleAccessor.DeleteClubRoleFromProfile(role3IdInTwo, profileId, _clubTwo.Id);
			profileRolesInTwo = _clubRoleAccessor.GetClubRolesByProfileId(profileId, _clubTwo.Id);
			NUnit.Framework.Assertion.Assert(profileRolesInTwo != null && profileRolesInTwo.Count == 0);

			UserRole role3 = profileRoles[0] as UserRole;
			NUnit.Framework.Assertion.Assert(role3.Name == "role3");
		}

		[NUnit.Framework.Test]
		public void testMessageAccessor()
		{
			int board1Id = _boardAccessor.Insert(new Board("Board 1",null,0,0,_clubOne.Id,BoardViewType.List));
			int board2Id = _boardAccessor.Insert(new Board("Board 2",null,0,0,_clubOne.Id,BoardViewType.List));

			Profile testProfile1 = _profileAccessor.Get(_profileAccessor.Insert(new Profile("userId1","userName 1", "password1","email1@mail.com")));
			Profile testProfile2 = _profileAccessor.Get(_profileAccessor.Insert(new Profile("userId2","userName 2", "password2","email2@mail.com")));
			Profile testProfile3 = _profileAccessor.Get(_profileAccessor.Insert(new Profile("userId3","userName 3", "password3","email3@mail.com")));


			int msg1Id = _messageAccessor.Insert(new Message("subject1","body1",testProfile1.UserName,testProfile1.UserId,testProfile1,board1Id,_clubOne.Id,0));
			int msg2Id = _messageAccessor.Insert(new Message("subject2","body2",testProfile1.UserName,testProfile1.UserId,testProfile1,board1Id,_clubOne.Id,0));
			int msg3Id = _messageAccessor.Insert(new Message("subject3","body3",testProfile1.UserName,testProfile1.UserId,testProfile1,board1Id,_clubOne.Id,0));
			int msg4Id = _messageAccessor.Insert(new Message("subject4","body4",testProfile1.UserName,testProfile1.UserId,testProfile1,board1Id,_clubOne.Id,0));
			int msg5Id = _messageAccessor.Insert(new Message("subject5","body5",testProfile1.UserName,testProfile1.UserId,testProfile1,board1Id,_clubOne.Id,0));
			int msg6Id = _messageAccessor.Insert(new Message("subject6","body6",testProfile1.UserName,testProfile1.UserId,testProfile1,board1Id,_clubOne.Id,0));
			int msg7Id = _messageAccessor.Insert(new Message("subject7","body7",testProfile1.UserName,testProfile1.UserId,testProfile1,board1Id,_clubOne.Id,0));

			int msg8Id = _messageAccessor.Insert(new Message("subject8","body8",testProfile1.UserName,testProfile1.UserId,testProfile1,board2Id,_clubOne.Id,0));
			Assert.AreEqual(8, _messageAccessor.GetMessageListByClubId(_clubOne.Id, 10, 1).Count);
			Assert.AreEqual(8, _messageAccessor.GetTotalRootMessageCountByClubId(_clubOne.Id));

			ArrayList messageList = _messageAccessor.GetMessageListByBoardId(board1Id,10,1);
			NUnit.Framework.Assertion.Assert(messageList.Count == 7 && _messageAccessor.GetTotalRootMessageCountByBoardId(board1Id) == 7);

			messageList = _messageAccessor.GetMessageListByBoardId(board1Id,5,2);
			NUnit.Framework.Assertion.Assert(messageList.Count == 2 && _messageAccessor.GetTotalRootMessageCountByBoardId(board1Id) == 7);

			//Author check
			Message newMessage2 = _messageAccessor.Get(msg2Id);
			Assert.IsNotNull(newMessage2.Author);
			Assert.AreEqual(testProfile1.UserId, newMessage2.Author.UserId);

			Message newMessage = _messageAccessor.Get(msg1Id);
			newMessage.Subject = "subject1 changed";
			newMessage.Body = "body1 changed";
			_messageAccessor.Update(newMessage);
			NUnit.Framework.Assertion.Assert(newMessage.Subject != "subject1" && newMessage.Body != "body1");

			int reply1_1Id = _messageAccessor.Insert(new Message("Re:subject1","Re:body1",testProfile2.UserName,testProfile2.UserId,testProfile2,board1Id,_clubOne.Id,msg1Id));
			int reply2_1Id = _messageAccessor.Insert(new Message("Re:subject1","Re:body1",testProfile2.UserName,testProfile2.UserId,testProfile2,board1Id,_clubOne.Id,msg1Id));
			int reply1_1_1Id = _messageAccessor.Insert(new Message("Rd:Re:subject1","Rd:Re:body1",testProfile3.UserName,testProfile3.UserId,testProfile3,board1Id,_clubOne.Id,reply1_1Id));
			Message rootMessage = _messageAccessor.Get(msg1Id);
			NUnit.Framework.Assertion.Assert(rootMessage.Replies.Count == 2);
			NUnit.Framework.Assertion.Assert(((Message)rootMessage.Replies[0]).Replies.Count == 1);

			_messageAccessor.Delete(msg1Id);
			messageList = _messageAccessor.GetMessageListByBoardId(board1Id,10,1);
			NUnit.Framework.Assertion.Assert(messageList.Count == 6 && _messageAccessor.GetTotalRootMessageCountByBoardId(board1Id) == 6);

			NUnit.Framework.Assertion.Assert(_messageAccessor.Get(reply1_1Id) == null);
			NUnit.Framework.Assertion.Assert(_messageAccessor.Get(reply2_1Id) == null);
			NUnit.Framework.Assertion.Assert(_messageAccessor.Get(reply1_1_1Id) == null);
		}
		[NUnit.Framework.Test]
		public void testAttachmentAccessor()
		{
			int board1Id = _boardAccessor.Insert(new Board("Board 1",null,0,0,_clubOne.Id,BoardViewType.List));
			Profile testProfile1 = _profileAccessor.Get(_profileAccessor.Insert(new Profile("userId1","userName 1", "password1","email1@mail.com")));
			int msg1Id = _messageAccessor.Insert(new Message("subject1","body1",testProfile1.UserName,testProfile1.UserId,testProfile1,board1Id,_clubOne.Id,0));
			int msg2Id = _messageAccessor.Insert(new Message("subject2","body2",testProfile1.UserName,testProfile1.UserId,testProfile1,board1Id,_clubOne.Id,0));

			int attachment1Id = _attachmentAccessor.Insert(new Attachment("attachment1",100,"testPath/test"),msg1Id);
			_attachmentAccessor.Insert(new Attachment("attachment1-2",50,"testPath/test"),msg1Id);
			int attachment2Id = _attachmentAccessor.Insert(new Attachment("attachment2",100,"testPath/test"),msg2Id);
			Message message1 = _messageAccessor.Get(msg1Id);
			Message message2 = _messageAccessor.Get(msg2Id);
			NUnit.Framework.Assertion.Assert(message1.Attachments.Count == 2 && message2.Attachments.Count == 1);
			
			_attachmentAccessor.Delete(attachment1Id);
			message1 = _messageAccessor.Get(msg1Id);
			_attachmentAccessor.Delete(attachment2Id);
			message2 = _messageAccessor.Get(msg2Id);
			NUnit.Framework.Assertion.Assert(message1.Attachments.Count == 1 && ((Attachment)message1.Attachments[0]).Name == "attachment1-2" && message2.Attachments.Count == 0);
		}

		[NUnit.Framework.Test]
		public void testCommentAccessor()
		{
			int board1Id = _boardAccessor.Insert(new Board("Board 1",null,0,0,_clubOne.Id,BoardViewType.List));
			Profile testProfile1 = _profileAccessor.Get(_profileAccessor.Insert(new Profile("userId1","userName 1", "password1","email1@mail.com")));
			int msg1Id = _messageAccessor.Insert(new Message("subject1","body1",testProfile1.UserName,testProfile1.UserId,testProfile1,board1Id,_clubOne.Id,0));
			int msg2Id = _messageAccessor.Insert(new Message("subject2","body2",testProfile1.UserName,testProfile1.UserId,testProfile1,board1Id,_clubOne.Id,0));

			int comment1Id = _commentAccessor.Insert(new Comment("comment1",testProfile1.UserName,testProfile1.UserId,testProfile1),msg1Id);
			_commentAccessor.Insert(new Comment("comment1-2",testProfile1.UserName,testProfile1.UserId,testProfile1),msg1Id);	
			int comment2Id = _commentAccessor.Insert(new Comment("comment2",testProfile1.UserName,testProfile1.UserId,testProfile1),msg2Id);

			Message message1 = _messageAccessor.Get(msg1Id);
			Message message2 = _messageAccessor.Get(msg2Id);
			NUnit.Framework.Assertion.Assert(message1.Comments.Count == 2 && message2.Comments.Count == 1);

			_commentAccessor.Delete(comment1Id);
			_commentAccessor.Delete(comment2Id);

			message1 = _messageAccessor.Get(msg1Id);
			message2 = _messageAccessor.Get(msg2Id);

			NUnit.Framework.Assertion.Assert(message1.Comments.Count == 1 && ((Comment)message1.Comments[0]).Body == "comment1-2" && message2.Comments.Count == 0);

		}

		[NUnit.Framework.Test]
		public void testPollItemAccessor()
		{

			int board1Id = _boardAccessor.Insert(new Board("Board 1",null,0,0,_clubOne.Id,BoardViewType.List));
			Profile testProfile1 = _profileAccessor.Get(_profileAccessor.Insert(new Profile("userId1","userName 1", "password1","email1@mail.com")));
			Profile testProfile2 = _profileAccessor.Get(_profileAccessor.Insert(new Profile("userId2","userName 2", "password2","email2@mail.com")));
			Profile testProfile3 = _profileAccessor.Get(_profileAccessor.Insert(new Profile("userId3","userName 3", "password3","email3@mail.com")));
			int msg1Id = _messageAccessor.Insert(new PollMessage("subject1","body1",testProfile1.UserName,testProfile1.UserId,testProfile1,board1Id,_clubOne.Id,0));
			int msg2Id = _messageAccessor.Insert(new PollMessage("subject2","body2",testProfile1.UserName,testProfile1.UserId,testProfile1,board1Id,_clubOne.Id,0));
			int msg3Id = _messageAccessor.Insert(new PollMessage("subject3","body3",testProfile1.UserName,testProfile1.UserId,testProfile1,board1Id,_clubOne.Id,0));

			int pollItem1_1Id = _pollItemAccessor.Insert(new PollItem("pollItem1-1"),msg1Id);
			int pollItem1_2Id = _pollItemAccessor.Insert(new PollItem("pollItem1-2"),msg1Id);
			int pollItem1_3Id = _pollItemAccessor.Insert(new PollItem("pollItem1-3"),msg1Id);
			int pollItem1_4Id = _pollItemAccessor.Insert(new PollItem("pollItem1-4"),msg1Id);

			int pollItem2_1Id = _pollItemAccessor.Insert(new PollItem("pollItem2-1"),msg2Id);
			int pollItem2_2Id = _pollItemAccessor.Insert(new PollItem("pollItem2-2"),msg2Id);
			int pollItem2_3Id = _pollItemAccessor.Insert(new PollItem("pollItem2-3"),msg2Id);

			int pollItem3_1Id = _pollItemAccessor.Insert(new PollItem("pollItem2-1"),msg3Id);
			int pollItem3_2Id = _pollItemAccessor.Insert(new PollItem("pollItem2-2"),msg3Id);

			PollMessage pollMessage1 = (PollMessage) _messageAccessor.Get(msg1Id);
			PollMessage pollMessage2 = (PollMessage) _messageAccessor.Get(msg2Id);
			PollMessage pollMessage3 = (PollMessage) _messageAccessor.Get(msg3Id);
			NUnit.Framework.Assertion.Assert(pollMessage1.PollItems.Count == 4 && pollMessage2.PollItems.Count == 3 && pollMessage3.PollItems.Count == 2);

			_pollItemAccessor.PostPoll(pollItem1_1Id,msg1Id,testProfile1.Id);
			_pollItemAccessor.PostPoll(pollItem1_2Id,msg1Id,testProfile2.Id);
			_pollItemAccessor.PostPoll(pollItem2_1Id,msg2Id,testProfile1.Id);
			_pollItemAccessor.PostPoll(pollItem2_2Id,msg2Id,testProfile2.Id);

			pollMessage1 = (PollMessage) _messageAccessor.Get(msg1Id);
			pollMessage2 = (PollMessage) _messageAccessor.Get(msg2Id);
			pollMessage3 = (PollMessage) _messageAccessor.Get(msg3Id);
			NUnit.Framework.Assertion.Assert(pollMessage1.DidUserVote(testProfile1.Id) && pollMessage1.DidUserVote(testProfile2.Id) && !pollMessage1.DidUserVote(testProfile3.Id) && pollMessage2.DidUserVote(testProfile1.Id) && pollMessage2.DidUserVote(testProfile2.Id) && !pollMessage2.DidUserVote(testProfile3.Id));
			foreach(PollItem item in pollMessage1.PollItems)
			{
				if(item.Id == pollItem1_1Id)
					NUnit.Framework.Assertion.Assert(item.NumberOfVotes == 1);
				else if(item.Id == pollItem1_2Id)
					NUnit.Framework.Assertion.Assert(item.NumberOfVotes == 1);
				else
					NUnit.Framework.Assertion.Assert(item.NumberOfVotes == 0);
			}

			_pollItemAccessor.Delete(pollItem3_1Id,msg3Id);
			pollMessage3 = (PollMessage) _messageAccessor.Get(msg3Id);

			NUnit.Framework.Assertion.Assert(pollMessage1.PollItems.Count == 4 && pollMessage2.PollItems.Count == 3 && pollMessage3.PollItems.Count == 1);

		}

		[NUnit.Framework.Test]
		[NUnit.Framework.ExpectedException(typeof(Exception))]
		public void testPollItemDelete()
		{
			int board1Id = _boardAccessor.Insert(new Board("Board 1",null,0,0,_clubOne.Id,BoardViewType.List));
			Profile testProfile1 = _profileAccessor.Get(_profileAccessor.Insert(new Profile("userId1","userName 1", "password1","email1@mail.com")));
			Profile testProfile2 = _profileAccessor.Get(_profileAccessor.Insert(new Profile("userId2","userName 2", "password2","email2@mail.com")));
			Profile testProfile3 = _profileAccessor.Get(_profileAccessor.Insert(new Profile("userId3","userName 3", "password3","email3@mail.com")));
			int msg1Id = _messageAccessor.Insert(new PollMessage("subject1","body1",testProfile1.UserName,testProfile1.UserId,testProfile1,board1Id,_clubOne.Id,0));

			int pollItem1_1Id = _pollItemAccessor.Insert(new PollItem("pollItem1-1"),msg1Id);
			int pollItem1_2Id = _pollItemAccessor.Insert(new PollItem("pollItem1-2"),msg1Id);
			int pollItem1_3Id = _pollItemAccessor.Insert(new PollItem("pollItem1-3"),msg1Id);
			int pollItem1_4Id = _pollItemAccessor.Insert(new PollItem("pollItem1-4"),msg1Id);

			PollMessage pollMessage1 = (PollMessage) _messageAccessor.Get(msg1Id);

			_pollItemAccessor.PostPoll(pollItem1_1Id,msg1Id,testProfile1.Id);
			_pollItemAccessor.PostPoll(pollItem1_2Id,msg1Id,testProfile2.Id);

			_pollItemAccessor.Delete(pollItem1_2Id, msg1Id);
		}
	}
}
